package com.greendragon.web.action.admin; 

import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.greendragon.control.constant.GlobalConstants;
import com.greendragon.persistence.domain.User;
import com.greendragon.persistence.enmu.FetchUserType;
import com.greendragon.service.backend.IUserBackendService;
import com.greendragon.web.action.BaseAction;

/** 
 * @author wulibin, exceptsky@gmail.com, 2012-1-5 上午09:09:40
 * 
 */
public class AccountInfoAction extends BaseAction{
	private static final long serialVersionUID = -4954077033863374527L;
	private IUserBackendService 	userService;
	
	private List<User> 				pageusers;									//一页的用户
	private long 					userCount;				 					//普通用户总数
	private long 					inactiveCount;								//未激活用户总数
	private long 					adminCount;									//管理员用户总数
	private long 					blockCount;									//冻结用户总数
	private int 					totalPage;									//总页数
	private String 					type;										//获取用户的类型
	private String 					page;										//当前页		
	private int 					pageLength = 15;
    private String					s;											//搜索内容
	
	public String accountInfo()throws Exception{
		initCount();
		userService.setPageLength(pageLength);
	
		int page = getPage(this.page);	//start from 0
		this.page = page+1+"";			//界面中页数从1开始
		FetchUserType fetchType = FetchUserType.getByName(type);
		
		if(StringUtils.isBlank(s)){
			if(fetchType == FetchUserType.USER){
				this.type = GlobalConstants.FETCH_USER_TYPE_USER;
				totalPage = (int)(userCount/pageLength)+1;
			}else if(fetchType == FetchUserType.ADMIN){
				totalPage = (int)(adminCount/pageLength)+1;
			}else if(fetchType == FetchUserType.BLOCK){
				totalPage = (int)(blockCount/pageLength)+1;
			}else if(fetchType == FetchUserType.INACTIVE){
				totalPage = (int)(inactiveCount/pageLength)+1;
			}
			pageusers = userService.getUsersByType(fetchType, page);
		}else{
			/*do search*/
			totalPage = (int)(userService.countOfSearch(s)/pageLength)+1;
			pageusers = userService.searchByName(s, page);
		}
		
		return SUCCESS;
	}
	
	private void initCount(){
		userCount = userService.getCountByType(FetchUserType.USER);
		adminCount = userService.getCountByType(FetchUserType.ADMIN);
		blockCount = userService.getCountByType(FetchUserType.BLOCK);
		inactiveCount = userService.getCountByType(FetchUserType.INACTIVE);
	}
	
	private int getPage(String pageStr){
		if(StringUtils.isBlank(pageStr)){
			return 0;
		}
		try{
			int p = Integer.parseInt(pageStr);
			if(p<1){
				return 0;
			}else{
				return p-1;
			}
		}catch(Exception e){
			return 0;
		}
	}

	public IUserBackendService getUserService() {
		return userService;
	}

	public void setUserService(IUserBackendService userService) {
		this.userService = userService;
	}

	public List<User> getPageusers() {
		return pageusers;
	}

	public void setPageusers(List<User> pageusers) {
		this.pageusers = pageusers;
	}

	public long getUserCount() {
		return userCount;
	}

	public void User(long userCount) {
		this.userCount = userCount;
	}

	public long getInactiveCount() {
		return inactiveCount;
	}

	public void setInactiveCount(long inactiveCount) {
		this.inactiveCount = inactiveCount;
	}

	public long getAdminCount() {
		return adminCount;
	}

	public void setAdminCount(long adminCount) {
		this.adminCount = adminCount;
	}

	public long getBlockCount() {
		return blockCount;
	}

	public void setBlockCount(long blockCount) {
		this.blockCount = blockCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPage() {
		return page;
	}
	
	public void setPage(String page) {
		this.page = page;
	}

	public int getPageLength() {
		return pageLength;
	}

	public void setPageLength(int pageLength) {
		this.pageLength = pageLength;
	}
	

	public String getS() {
		return s;
	}

	public void setS(String s) {
		s = s.trim();
		if(s.length()>10){
			s = s.substring(0,10);
		}
		this.s = s;
		
	}
}
 